package com.controller;

import com.alibaba.fastjson.JSON;
import com.entity.Role;
import com.entity.User;
import com.mysql.cj.Session;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public String user(@RequestParam(required = true) String account, @RequestParam(required = true) String password, Model model, HttpSession session){
        User user = userService.user(account,password);
        if (user!=null){
            session.setAttribute("userSession",user);
            return "frame";
        }else {
            model.addAttribute("error","账号或密码错误，请重新输入！");
            return "login";
        }
    }
    @RequestMapping("/sys/toMain")
    public String insert(){
        return "frame";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("userSession");
        return "login";
    }

    @RequestMapping("/user/list")
    public ModelAndView finUser(){
        ModelAndView modelAndView = new ModelAndView();
        List<User> list = userService.finUser();
        modelAndView.addObject("userList",list);
        modelAndView.setViewName("user/list");
        return modelAndView;
    }

    @RequestMapping("/user/toUpdatePwd")
    public String toUpdatePwd(){
        return "user/updatePassword";
    }

    @RequestMapping("/user/checkOldPwd")
    @ResponseBody
    public String checkOldPwd(@RequestParam(required = true) String password,HttpSession session){
     User user = (User) session.getAttribute("userSession");
     if (password.equals(user.getPassword())){
         return "{\"result\":\"true\"}";
     }else {
        return "{\"result\":\"false\"}";
     }
    }

    @RequestMapping("/user/updatePassword")
    public String updatePassword(@RequestParam(required = true) String newpassword,HttpSession session,Model model){
        User user = (User) session.getAttribute("userSession");
        int update = userService.updatePassword(newpassword,user.getId());
        if (update>0){
           return "frame";
        }else {
            model.addAttribute("name","修改失败");
            return"user/updatePassword";
        }
    }

    @RequestMapping("user/view")
    public String userId(@RequestParam(required = true)int id, Model model,String name){
        User user = userService.userId(id);
        model.addAttribute("user",user);
        if (name.equals("查看")) {
            return "user/view";
        }else {
            return "user/update";
        }
    }

    @RequestMapping("user/update")
    public String updateUser(User user){
        int update = userService.updateUser(user);
        if (update>0){
            return "frame";
        }else {
            return "user/update";
        }

    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "user/add";
    }

    @RequestMapping("user/add")
    public String add(User user){
        int add = userService.insertUser(user);
        if (add>0){
            return "frame";
        }else {
            return "user/update";
        }
    }

    @RequestMapping("user/wher")
    public ModelAndView wher(String userAccount,String userName){
        ModelAndView modelAndView = new ModelAndView();
        List<User> list = userService.listUser(userAccount,userName);
        modelAndView.addObject("userList",list);
        modelAndView.setViewName("user/list");
        return modelAndView;
    }
}
